<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" style="height:600px" :xs="24">
        <div class="head-container">
          <!--          <el-select v-model="value" placeholder="请选择主题域" :filterable="true" @change="getSelectValue">-->
          <!--            <el-option-->
          <!--              v-for="item in options"-->
          <!--              :key="item.zoneTitle"-->
          <!--              :label="item.zoneTitle"-->
          <!--              :value="item.id">-->
          <!--            </el-option>-->
          <!--          </el-select>-->
          <!--          <div class="theme_left_list">-->
          <!--            <span v-for="item in options" class="theme_span" @click="getSelectValue(item.id)">{{item.zoneTitle}}</span>-->
          <!--          </div>-->
          <el-input
            v-model="queryParams.zoneTitle"
            placeholder="搜索主题域"
            clearable
            size="small"
            @change="handleQuery"
          />
          <el-table
            border
            ref="singleTable"
            :data="options"
            highlight-current-row
            @row-click="rowClick"
            class="setList"
             >
            <af-table-column
            show-overflow-tooltip
              property="zoneTitle"
              label="主题域列表"
            >
            </af-table-column>
          </el-table>
          <pagination
            small
            hide-on-single-page
            v-show="totalCount>0"
            :total="totalCount"
            :pager-count="5"
            layout="prev, pager, next"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-row>
          <el-col :span="5.0">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleInitAdd"
            >初始新增
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-table
            :data="tableData"
            style="width: 100%;margin-bottom: 20px;"
            row-key="id"
            border
            default-expand-all
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
              prop="id"
              label="编号"
              sortable
              width="180"
              is="false">
            </el-table-column>
            <el-table-column
            align="left"
              prop="setTitle"
              label="标题"
              sortable
              width="300">
            </el-table-column>

            <el-table-column
              align="center"
              prop="orderNum"
              label="排序"
              width="80">
            </el-table-column>

            <el-table-column
            align="center"
              prop="createBy"
              label=创建人
              width="80">
            </el-table-column>
            <el-table-column align="center" label="是否为主题" width="90">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.isTheme"
                  active-color="#13ce66"
                  disabled
                  inactive-color="#ff4949">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column
              prop="createDate"
              align="center"
              label="创建时间">
            </el-table-column>
            <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini"
                           type="text"
                           icon="el-icon-edit"
                           @click="handleUpdate(scope.row)"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                  @click="handleAdd(scope.row)"
                >新增
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>

        <!-- 添加或修改set对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="标题" prop="zoneTitle">
              <el-input v-model="form.setTitle" placeholder="请输入主题集标题"/>
            </el-form-item>
            <el-form-item label="描述" prop="zoneDescription">
              <el-input v-model="form.setDescription" placeholder="请输入主题集描述" type="textarea"/>
            </el-form-item>
            <el-form-item label="排序" prop="orderNum">
              <el-input v-model.number="form.orderNum" placeholder="请输入排序序号"/>
            </el-form-item>
            <!--            <el-form-item label="主题" prop="orderNum">-->
            <!--              <el-switch-->
            <!--                v-model="form.isTheme"-->
            <!--                active-color="#13ce66"-->
            <!--                inactive-color="#ff4949">-->
            <!--              </el-switch>-->
            <!--            </el-form-item>-->
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>

import { listZone } from '@/api/quota/theme_zone/zone.js'
import { addSet, delSet, getSet, getThemeSetTree, updateSet } from '@/api/quota/theme_zone/set.js'
import Cookies from 'js-cookie'

export default {
  name: 'Set',
  data() {
    return {
      currentRow: null,
      switchValue: 0,
      zoneId: undefined,
      totalCount: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zoneTitle: undefined,
        orderField: 'ORDER_NUM',
        order: 'asc'
      },
      options: [],
      value: '',
      tableData: [],
      title: '',
      open: false,
      form: {},
      rules: {
        setTitle: [
          { required: true, message: '主题集标题不能为空', trigger: 'blur' }
        ],
        setDescription: [
          { required: true, message: '主题集描述不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '该字段只能填写数字', trigger: 'blur' },
          { type: 'number', message: '排序必须为数字值' }
        ]
      }
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    rowClick(row, column, event) {
      this.getSelectValue(row.id)
    },
    getList() {
      listZone(this.queryParams).then(response => {
        this.options = response.data.list
        this.totalCount = response.data.totalCount
      })
    },
    getSelectValue(value) {
      getThemeSetTree(value).then((res) => {
        this.tableData = res.data
        this.selectValue = value
      }).catch(() => {
        this.msgSuccess('暂无数据')
      })
    },
    reset() {
      this.form = {
        setTitle: undefined,
        setDescription: undefined,
        orderNum: 1,
        parentId: 0,
        zoneId: this.selectValue,
        isTheme: true,
        createBy: Cookies.get('username')
      }
      this.resetForm('form')
    },
    handleAdd(row) {
      this.reset()
      this.open = true
      this.title = '添加主题集'
      // if(this.form.isTheme===false){
      //   this.open = false;
      //   this.msgError('无法添加')
      // }
      if (row != null) {
        this.form.parentId = row.id
        // if (row.parentId == 0) {
        //   this.form.parentId = row.id;
        // }
        console.log('form:', this.form)
      }
    },
    handleInitAdd() {
      this.reset()
      this.open = true
      this.title = '添加主题集'
      if (this.selectValue == undefined) {
        this.open = false
        this.msgError('请选择主题域在进行下一步')
      }
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSet(id).then(response => {
        console.log(response)
        this.form = response.data
        this.open = true
        this.title = '修改主题集'
      })
    },
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSet(this.form).then(response => {
              if (response.data === true) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getSelectValue(this.selectValue)
              }
            })
          } else {
            addSet(this.form).then(response => {
              if (response.data === true) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getSelectValue(this.selectValue)
              }
            })
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.switchValue = 0
      this.reset()
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除主题集编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSet(ids)
      }).then((res) => {
        if (res.data === 0) {
          this.msgError('删除失败')
        } else {
          this.getSelectValue(this.selectValue)
          this.msgSuccess('删除成功')
        }

      }).catch(function() {
      })
    }
  }
}
</script>
<style lang="less" scoped>
.theme_left_list {
  width: 100%;
  display: flex;
  flex-direction: column;
  margin-top: 30px;
}
.setList.el-table{
  /deep/tbody {
    display: inline-block;
    min-width: 100%;
  }
  /deep/.cell{
    white-space:nowrap;
}
  /deep/td{
  display: block;
  min-width: 100%;

}
  /deep/.el-table__row{
    display: block;
    min-width: 100%;
}
}
.theme_span {
  width: 100%;
  display: flex;
  flex-direction: column;
  padding: 5px 15px;
  background: #f8f8f9;
  cursor: pointer;
  margin-bottom: 2px;
  color: #1890ff;
}

.el-row {
  margin-bottom: 10px;
}
</style>
